package com.zimmur.app.mocolink.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zimmur.app.mocolink.dao.GroupGoodsItemMapper;
import com.zimmur.app.mocolink.dao.GroupGoodsMapper;
import com.zimmur.app.mocolink.domain.model.GroupGoods;
import com.zimmur.app.mocolink.domain.model.GroupGoodsItem;
import com.zimmur.app.mocolink.domain.model.GroupGoodsItemExample;
import com.zimmur.app.mocolink.domain.param.GroupGoodsParam;
import com.zimmur.app.mocolink.domain.param.TopInGroupGoodsReqParam;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.vo.GroupGoodsForManageVo;
import com.zimmur.app.mocolink.domain.vo.GroupGoodsInfo;
import com.zimmur.app.mocolink.domain.vo.GroupGoodsManageVo;
import com.zimmur.app.mocolink.domain.vo.GroupGoodsVo;
import com.zimmur.app.mocolink.service.IGroupGoodsService;

@Service
public class GroupGoodsServiceImpl implements IGroupGoodsService {

	@Autowired
	private GroupGoodsMapper groupGoodsMapper;
	@Autowired
	private GroupGoodsItemMapper groupGoodsItemMapper;
	@Override
	public PageUtil<GroupGoodsInfo> queryTopInGroupGoods(TopInGroupGoodsReqParam param) {
		PageUtil<GroupGoodsInfo> page = new PageUtil<GroupGoodsInfo>();
		int count = groupGoodsMapper.countByGroupGoodsReqParam(param);
		List<GroupGoodsInfo> list = groupGoodsMapper.readByGroupGoodsReqParam(param);
		page.setTotalRecord(count);
		page.setPageNo(param.getPageIndex());
		page.setPageSize(param.getPageSize());
		page.setItemList(list);
		return page;
	}
	@Override
	public PageUtil<GroupGoodsInfo> queryIndexGroupGoods(int pageNo, int userId) {
		PageUtil<GroupGoodsInfo> page = new PageUtil<>();
		page.setPageNo(pageNo);
		
		List<GroupGoodsInfo> groupGoodsInfos=groupGoodsMapper.selectRecommendGroup(page.getPageBeginNo(), page.getPageSize(), userId);
		int count = groupGoodsMapper.countRecommendGroup();
		page.setItemList(groupGoodsInfos);
		page.setTotalRecord(count);
		
		return page;
	}
	@Override
	public GroupGoods selectById(Integer groupGoodsId) {
		return groupGoodsMapper.selectByPrimaryKey(groupGoodsId);
	}
	@Override
	public int update(GroupGoods groupGoods) {
		return groupGoodsMapper.updateByPrimaryKeySelective(groupGoods);
	}
	@Override
	public GroupGoodsVo selectGroupGoodsVo(Integer groupGoodsId, Integer userId) {
		
		return groupGoodsMapper.selectGroupGoodsVo(userId, groupGoodsId);
	}
	@Override
	public PageUtil<GroupGoodsInfo> queryInfoByParam(GroupGoodsParam param) {
		PageUtil<GroupGoodsInfo> page = new PageUtil<>();
		List<GroupGoodsInfo> list = groupGoodsMapper.selectInfoByParam(param);
		int count = groupGoodsMapper.countInfoByParam(param);
		page.setPageNo(param.getPageNo());
		page.setPageSize(param.getPageSize());
		page.setItemList(list);
		page.setTotalRecord(count);
		return page;
	}
	@Override
	public PageUtil<GroupGoodsInfo> queryInfoByParamForManage(GroupGoodsParam param) {
		PageUtil<GroupGoodsInfo> page = new PageUtil<>();
		List<GroupGoodsInfo> list = groupGoodsMapper.selectInfoByParamForManage(param);
		int count = groupGoodsMapper.countInfoByParamForManage(param);
		page.setPageNo(param.getPageNo());
		page.setPageSize(param.getPageSize());
		page.setItemList(list);
		page.setTotalRecord(count);
		return page;
	}
	@Transactional
	@Override
	public Integer deleteGroupGoods(Integer groupId) {
		//1.删除组货信息
		GroupGoods  ggood = groupGoodsMapper.selectByPrimaryKey(groupId);
		if(ggood!=null){
			groupGoodsMapper.deleteByPrimaryKey(groupId);
		}
		Integer flag = 0;
		//2.删除组货明细
		GroupGoodsItemExample example = new GroupGoodsItemExample();
		example.createCriteria().andGroupGoodsIdEqualTo(groupId);
		List<GroupGoodsItem> itemList = groupGoodsItemMapper.selectByExample(example);
		if(itemList!=null&&!itemList.isEmpty()){
			flag = groupGoodsItemMapper.deleteByExample(example);
		}
		return flag;
	}
	@Override
	public GroupGoodsForManageVo queryGroupGoodsInfo(Integer groupId) {
		GroupGoodsForManageVo vo = new GroupGoodsForManageVo();
		//1.查询组货信息
		GroupGoods groupGoods = groupGoodsMapper.selectByPrimaryKey(groupId);
		vo.setCoverImage(groupGoods.getCoverImage());
		vo.setCustomer(groupGoods.getCustomer());
		vo.setDesc(groupGoods.getDescription());
		vo.setGroupGoodsId(groupGoods.getId());
		vo.setScene(groupGoods.getScene());
		vo.setStyle(groupGoods.getStyle());
		vo.setTotalPrice(groupGoods.getTotalPrice());
		vo.setIsRecommend(groupGoods.getIsRecommend());
		//2.查询组货明细
		GroupGoodsItemExample  example = new GroupGoodsItemExample();
		example.createCriteria().andGroupGoodsIdEqualTo(groupId);
		List<GroupGoodsItem> item = groupGoodsItemMapper.selectByExample(example);
		vo.setGoodsItemList(item);
		return vo;
	}
	@Override
	public int updateGroupGoodsForManage(GroupGoodsManageVo vo, String userName) {
		//1.修改组货信息
		GroupGoods ggoods = new GroupGoods();
		ggoods.setCoverImage(vo.getImage());
		ggoods.setCustomer(vo.getCustomer());
		ggoods.setDescription(vo.getDesc());
		ggoods.setId(vo.getGroupGoodsId());
		ggoods.setIsRecommend(vo.getIsRecommend());
		ggoods.setScene(vo.getScene());
		ggoods.setStyle(vo.getStyle());
		ggoods.setUpdateTime(new Date());
		ggoods.setUpdator(userName);
		Integer flag = 0;
		BigDecimal totalPrice = new BigDecimal(0);
		List<GroupGoodsItem> gGitem = vo.getGoodsItemList();
		if(gGitem!=null&&!gGitem.isEmpty()){
			for(GroupGoodsItem item: gGitem){
				totalPrice = item.getGoodsPrice().add(totalPrice);
			}
			ggoods.setTotalPrice(totalPrice);
			flag = groupGoodsMapper.updateByPrimaryKeySelective(ggoods);
		}
		//2.修改组货明细
		if(flag==1){
			//2.1.先删除原有的组货明细
			GroupGoodsItemExample  example = new GroupGoodsItemExample();
			example.createCriteria().andGroupGoodsIdEqualTo(vo.getGroupGoodsId());
			List<GroupGoodsItem> itemList = groupGoodsItemMapper.selectByExample(example);
			if(itemList!=null&&!itemList.isEmpty()){
				groupGoodsItemMapper.deleteByExample(example);
			}
			//2.2.新增组货明细
			for(GroupGoodsItem item:gGitem){
				item.setGroupGoodsId(vo.getGroupGoodsId());
				groupGoodsItemMapper.insertSelective(item);
			}
		}
		return flag;
	}
	@Override
	public int saveGroupGoodsForManage(GroupGoodsManageVo vo, String userName) {
		//1.新增组货信息
		GroupGoods ggoods = new GroupGoods();
		ggoods.setCoverImage(vo.getImage());
		ggoods.setCustomer(vo.getCustomer());
		ggoods.setDescription(vo.getDesc());
		ggoods.setId(vo.getGroupGoodsId());
		ggoods.setIsRecommend(vo.getIsRecommend());
		ggoods.setScene(vo.getScene());
		ggoods.setStyle(vo.getStyle());
		ggoods.setCreateTime(new Date());
		ggoods.setCreator(userName);
		Integer flag = 0;
		BigDecimal totalPrice = new BigDecimal(0);
		List<GroupGoodsItem> gGitem = vo.getGoodsItemList();
		if(gGitem!=null&&!gGitem.isEmpty()){
			for(GroupGoodsItem item: gGitem){
				totalPrice = item.getGoodsPrice().add(totalPrice);
			}
			ggoods.setTotalPrice(totalPrice);
			flag = groupGoodsMapper.insertSelective(ggoods);
		}
		//2.新增组货明细
		if(flag==1){
			Integer seq = ggoods.getId();
			for(GroupGoodsItem item: gGitem){
				item.setGroupGoodsId(seq);
				groupGoodsItemMapper.insertSelective(item);
			}
		}
		return flag;
	}
}
